class Solution(object):
    def maximumGain(self, s, x, y):
        st = []
        ans = 0
        if x >= y:
            for c in s:
                if c == 'b' and st and st[-1] == 'a':
                    st.pop()
                    ans += x
                else:
                    st.append(c)
            new_st = []
            for c in st:
                if c == 'a' and new_st and new_st[-1] == 'b':
                    new_st.pop()
                    ans += y
                else:
                    new_st.append(c)
        else:
            for c in s:
                if c == 'a' and st and st[-1] == 'b':
                    st.pop()
                    ans += y
                else:
                    st.append(c)
            new_st = []
            for c in st:
                if c == 'b' and new_st and new_st[-1] == 'a':
                    new_st.pop()
                    ans += x
                else:
                    new_st.append(c)
        return ans

data = Solution()
s = "cdbcbbaaabab"
x = 4
y = 5
print(data.maximumGain(s, x, y))
s = "cbaabwbbbabbwaaq"
x = 1
y = 2
print(data.maximumGain(s, x, y))
